package com.imagine.controller.client;

import com.imagine.entity.*;
import com.imagine.service.AccountStockService;
import com.imagine.service.CapitalAccountService;
import com.imagine.service.LegalPersonalSecurityAccountService;
import com.imagine.service.PersonalSecurityAccountService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by liangyue on 2017/9/26.
 */

@RestController
@EnableAutoConfiguration
@RequestMapping("/account")
public class CapitalAccountController {

    @Autowired
    private CapitalAccountService capitalAccountService;
    @Autowired
    private PersonalSecurityAccountService personalSecurityAccountService;

    @Autowired
    private LegalPersonalSecurityAccountService legalPersonalSecurityAccountService;

    @Autowired
    private AccountStockService accountStockService;


    @ResponseBody
    @ApiOperation(value="用户资金账户登录", notes="用户资金账户登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public BaseResult capitalAccountLogin(
            @ApiParam("资金账户名称") @RequestParam(value = "name") String name,
            @ApiParam("密码") @RequestParam(value = "password") String password,
            HttpSession session
    ) throws IOException {
        BaseResult result = new BaseResult();
        if (capitalAccountService.getCapitalAccountById(name) == null){
            result.error("没有该用户信息");
            return result;
        }
        CapitalAccount account = capitalAccountService.getCapitalAccountById(name);

        if (!account.getPassword().equals(password)){
            result.error("密码错误");
            return result;
        }
        session.setAttribute("account", account);
        result.setMessage("登录成功");
        return result;
    }


    @ResponseBody
    @ApiOperation(value="获得用户资金信息", notes="获得资金用户信息")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public BaseResult getCapitalAccountInfo(HttpSession session) throws IOException {
        BaseResult result = new BaseResult();
        CapitalAccount  account = (CapitalAccount) session.getAttribute("account");
        result.success(account);
        return result;
    }




    @ResponseBody
    @ApiOperation(value="创建新资金用户", notes="创建新资金用户")
    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public BaseResult insertCapitalAccount(
            @ApiParam("证券账户ID") @RequestParam(value = "sAccountId") int sAccountId,
            @ApiParam("证券账户类型") @RequestParam(value = "accountType") int accountType,
            @ApiParam("密码") @RequestParam(value = "password") String password,
            @ApiParam("资金总余额") @RequestParam(value = "remainAmount") double remainAmount,
            @ApiParam("可用资金余额") @RequestParam(value = "usableRemainAmount") double usableRemainAmount
    ) throws IOException {
        BaseResult result = new BaseResult();
        CapitalAccount capitalAccount;
        String cAccountId;
        if (accountType == 0){
            cAccountId = "000"+sAccountId;
            capitalAccount = new CapitalAccount(cAccountId, sAccountId, 0,
                    accountType,remainAmount,usableRemainAmount,null,password);
            if (personalSecurityAccountService.getPersonalSecurityAccountById(capitalAccount.getsAccountId())==null
                    ){
                result.error("该个人账户未在交易所注册");
                return result;
            }

        }else {
            cAccountId = "111"+sAccountId;
            capitalAccount = new CapitalAccount(cAccountId, sAccountId, 0,
                    accountType,remainAmount,usableRemainAmount,null, password);
            if (legalPersonalSecurityAccountService.getlAccountByLAccountId(capitalAccount.getsAccountId())==null){
                result.error("该法人账户未在交易所注册");
                return result;
            }
        }

        if(capitalAccountService.getCapitalAccountById(cAccountId)!=null){
            result.error("该资金账户已经开通");
            return result;
        }

        capitalAccountService.insertCapitalAccount(capitalAccount);
        CapitalAccount capitalAccount1 = capitalAccountService.getCapitalAccountById(cAccountId);
        result.success(capitalAccount1);
        return result;
    }


    @ResponseBody
    @ApiOperation(value="根据用户资金账户ID获得所有用户持仓", notes="根据用户资金账户ID获得所有用户持仓")
    @RequestMapping(value = "/stock", method = RequestMethod.GET)
    public BaseResult<Object> getDelegationsByCId(HttpSession session) throws IOException {
        CapitalAccount capitalAccount = (CapitalAccount) session.getAttribute("account");
        System.out.println(capitalAccount);
        return accountStockService.getAllStockByCId(capitalAccount.getcAccountId());
    }

    @ResponseBody
    @ApiOperation(value="根据用户资金账户ID获得所有用户资金流动", notes="根据用户资金账户ID获得所有用户资金流动")
    @RequestMapping(value = "/capitalflow", method = RequestMethod.POST)
    public BaseResult<Object> getCapitalFlowByCId(HttpSession session) throws IOException {
        CapitalAccount capitalAccount = (CapitalAccount) session.getAttribute("account");
        return capitalAccountService.getCapitalFlowByCId(capitalAccount.getcAccountId());
    }

    @ResponseBody
    @ApiOperation(value="添加自选股", notes="添加自选股")
    @RequestMapping(value = "/option/new", method = RequestMethod.POST)
    public BaseResult insertOptionalStock(
            @ApiParam("股票代码") @RequestParam(value = "securityId") String securityId,HttpSession session
    ) throws IOException {
        CapitalAccount capitalAccount = (CapitalAccount) session.getAttribute("account");
        return capitalAccountService.insertOptionalStock(capitalAccount.getcAccountId(), securityId);
    }

    @ResponseBody
    @ApiOperation(value="删除自选股", notes="删除自选股")
    @RequestMapping(value = "/option/deletion", method = RequestMethod.POST)
    public BaseResult deleteOptionalStock(
            @ApiParam("股票代码") @RequestParam(value = "securityId") String securityId,HttpSession session
    ) throws IOException {
        CapitalAccount capitalAccount = (CapitalAccount) session.getAttribute("account");
        return capitalAccountService.deleteOptionalStock(capitalAccount.getcAccountId(), securityId);
    }

    @ResponseBody
    @ApiOperation(value="查询所有的自选股", notes="查询所有的自选股")
    @RequestMapping(value = "/option/all", method = RequestMethod.POST)
    public BaseResult queryAllOptionalStock(HttpSession session) throws IOException {
        CapitalAccount capitalAccount = (CapitalAccount) session.getAttribute("account");
        return capitalAccountService.getAllOptionalStocks(capitalAccount.getcAccountId());
    }
}


















